# 1.导包
from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference

# 2.实例化
wb = Workbook()
sheet = wb.active

# 3.准备饼状图的数据
data = [
    ['类', '销量'],
    ['苹果', 50],
    ['樱桃', 30],
    ['南瓜', 10],
    ['巧克力', 40],
]
for v in data:
    sheet.append(v)

# 4.创建饼状图
p1 = PieChart()
p1.title = '水果销量表'



# 添加  数据 --饼状图

data = Reference(sheet, min_row=1, min_col=2, max_row=5)
p1.add_data(data, titles_from_data=True)

# 添加 右侧 标注
lables = Reference(sheet, min_row=2, min_col=1, max_row=5)
p1.set_categories(lables)


# 设置饼状图的样式
# 1.导入模块 datapoint
from openpyxl.chart.series import DataPoint

# 2.设置   哪个饼子 分离多远的点位
# idx 决定 到底是哪个类 分离
# explosion 决定了 离圆心的距离
slice = DataPoint(idx=2, explosion=50)
# 3.设置位置
p1.series[0].data_points = [slice]

# 将饼状图  写入 sheet
sheet.add_chart(p1,'D1')

# 3.保存工作簿
wb.save('01pie.xlsx')

print('饼状图....')
